// pages/linkanalysis/linkanalysis.js

const common = require('../../mixins/common');
const forum = require('../../mixins/forum')
const app = getApp();
const options = {
  /**
   * 页面的初始数据
   */
  data: {
    linktype: '',
    imagesubject: [],
    openmap: false,
    tags: [],
    type:'forum',
    anonymity:true,
    mediaType:0,
    cats:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCircleLists({
      'custom': 'sticky'
    });
  },

  /**
   * 监听链接输入
   */
  changeLink: function (e) {
    var link = e.detail.value;
    this.setData({
      link: link
    })
  },

  /**
   * 是否匿名
   */
  changeSwitch: function (e) {
    console.log(e)
    let value = e.detail.value;
    this.setData({
      anonymity: value
    })
  },


  
  /**
   * 快速选择圈子
   */
  selectTopic: function (e) {
    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.id;
    this.setData({
      topic: name,
      topicid: id,
    });
  },

  /**
   * 新增自定义选项
   */
  onShopClick: function () {
    wx.navigateTo({
      url: '/pages/creat/shoplist'
    })
  },


  /**
   * 解析链接
   */

  anAlysis: function () {
    if (!this.data.link) {
      wx.showToast({
        title: '请输入链接',
      })
    }
    this.jumpAnalysis({
      url: this.data.link
    })

  },
  /**
   * 清除链接
   */

  deleteLink:function(){
  this.setData({
    link:'',
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onTopicClick: function () {
    wx.navigateTo({
      url: '/pages/creat/circle',
    })

  },

  
  onLocationClick: function () {
    var t = this;
    this.checkPermission("scope.userLocation", "请到设置页面授权“位置信息”权限", function () {
      t.startChooseLocation();
    });
  },

  startChooseLocation: function () {
    var t = this;
    wx.chooseLocation({
      success: function (a) {
        t.setData({
          location: {
            latitude: a.latitude,
            longitude: a.longitude,
            address: a.address,
            name: a.name
          }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user = app.globalData.user
    let that = this;
    if (!user) {
      user = '';
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      user: user,
    })
    wx.getClipboardData({
      success: function (res) {
        var data = that.confirmvideo(res.data);
        if (data) {
          that.setData({
            link: data
          })
        }
      }
    })
  },

  onTagClick: function () {
    wx.navigateTo({
      url: '/pages/creat/tags?tags=' + JSON.stringify(this.data.tags)
    })
  },

  
  /**
   * 识别链接
   */


  confirmvideo: function(url) {
    let miaopai = RegExp(/http\:\/\/n\.miaopai\.com\/media\/(.*?)\.htm/)
    let douyin = RegExp(/https\:\/\/v\.douyin\.com\/(.*?)\//)
    let weibo = RegExp(/https\:\/\/m\.weibo\.cn\/(.*?)\/(.*?)/)
    let weixin = RegExp(/https\:\/\/mp\.weixin\.qq\.com\/(.*?)\/(.*?)/)
    let bilibili = RegExp(/https\:\/\/www\.bilibili\.com\/read\/cv(.*?)/)
    let xiaohongshu = RegExp(/http\:\/\/t\.cn\/(.*?)/)
    let vuevideo = RegExp(/https\:\/\/v\.vuevideo\.net\/share\/post\/(.*?)/)
    let zhihu = RegExp(/https\:\/\/zhuanlan\.zhihu\.com\/p\/(.*?)/)
    let qutoutiao = RegExp(/http\:\/\/new\.3qtt\.cn\/(.*?)/)
    let jianshu = RegExp(/https\:\/\/www\.jianshu\.com\/p\/(.*?)/)
    let qqnews = RegExp(/https\:\/\/view\.inews\.qq\.com\/a\/(.*?)\?/)
    let biliTv = RegExp(/https\:\/\/b23\.tv\/(.*?)/)
    let douban = RegExp(/https\:\/\/www\.douban\.com\/doubanapp\/dispatch\?uri\=\/note\/(.*?)\//)
    if (miaopai.exec(url)) {
      this.setData({
        sharetype: 'video',
        linktype: "检测到秒拍视频链接"
      })
      return url;
    } else if (weibo.exec(url)) {
      this.setData({
        sharetype: 'image',
        linktype: "检测到微博链接"
      })
      return url;
    } else if (xiaohongshu.exec(url)) {
      url = url + '"';
      let xiaohongshulink = url.match(/http\:\/\/t\.cn\/(.*?)\"/);
      console.log(xiaohongshulink);
      this.setData({
        sharetype: 'image',
        linktype: "检测到小红书链接"
      })
      return "http://t.cn/" + xiaohongshulink[1];
    } else if (vuevideo.exec(url)) {
      url = url + '"';
      let vuelink = url.match(/https\:\/\/v\.vuevideo\.net\/share\/post\/(.*?)"/);
      this.setData({
        sharetype: 'video',
        linktype: "检测到Vuevideo链接"
      })
      return "https://v.vuevideo.net/share/post/" + vuelink[1];
    } else if (weixin.exec(url)) {
      this.setData({
        sharetype: 'link',
        linktype: "检测到公众号链接"
      })
      return url;
    } else if (douyin.exec(url)) {
      let douyinlink = url.match(/https\:\/\/v\.douyin\.com\/(.*?)\//);
      let videoLink = "https://v.douyin.com/" + douyinlink[1] + "/"
      this.setData({
        sharetype: 'video',
        linktype: "检测到抖音链接"
      })
      return videoLink;
    } else if (bilibili.exec(url)) {
      this.setData({
        sharetype: 'link',
        linktype: "检测到bilibili链接"
      })
      let bilibililink = url.match(/https\:\/\/www\.bilibili\.com\/read\/cv(.*?)\?/);
      return "https://www.bilibili.com/read/cv" + bilibililink[1];
    } else if (zhihu.exec(url)) {
      this.setData({
        sharetype: 'link',
        linktype: "检测到知乎专栏链接"
      })
      return url;
    } else if (qutoutiao.exec(url)) {
      this.setData({
        sharetype: 'link',
        linktype: "检测到趣头条链接"
      })
      return url;
    } else if (jianshu.exec(url)) {
      this.setData({
        sharetype: 'link',
        linktype: "检测到简书链接"
      })
      return url;
    } else if (biliTv.exec(url)) {
      console.log('bilibili')
      this.setData({
        sharetype: 'link',
        linktype: "检测到B站视频链接"
      })
      return url;
    } else if (douban.exec(url)) {
      this.setData({
        sharetype: 'link',
        linktype: "检测豆瓣链接"
      })
      let doubanlink = url.match(/https\:\/\/www\.douban\.com\/doubanapp\/dispatch\?uri\=\/note\/(.*?)\//);
      let videoLink = "https://m.douban.com/note/" + doubanlink[1] + "/";
      return videoLink;
    } else {
      return false;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}

common(options)
forum(options)
Page(options)